//
//  CustomPresentController.swift
//  firstSwiftWeiBo
//
//  Created by Inverno on 16/12/12.
//  Copyright © 2016年 Inverno. All rights reserved.
//

import UIKit

class CustomPresentController: UIPresentationController {
    
    private lazy var coverView : UIView = UIView()
    
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        // 1.设置弹出view的尺寸
        presentedView()?.frame = CGRect(x: 100, y: 55, width: 120, height: 200)
        
        // 2.添加蒙版
        setupCoverView()
    }
}

extension CustomPresentController {
    private func setupCoverView() {
        // 1.添加蒙版
        containerView?.insertSubview(coverView, atIndex: 0)
        
        // 2.设置蒙版属性
        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)
        coverView.frame = containerView?.bounds ?? CGRectZero
        
        // 3.添加收拾
        let gesture = UITapGestureRecognizer(target: self, action: "coverViewClicked")
        coverView.addGestureRecognizer(gesture)
    }
}

extension CustomPresentController {
    @objc private func coverViewClicked() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}